import { ref } from 'vue'
import utils from 'xe-utils'

export function useForm<T>(initFormData: T) {
  const formRef = ref()
  const formModel = ref<T>({ ...initFormData })

  const rules = {}

  const validation = () => {
    return formRef.value?.validate()
  }

  const restFields = () => {
    return formRef.value?.resetFields()
  }

  const initForm = (form?: any) => {
    formModel.value = utils.destructuring({ ...initFormData }, form || {}) as any
  }

  return {
    formRef,
    formModel,
    validation,
    rules,
    initForm,
    restFields,
  }
}
